package com.vehicle_demo.utils;

import org.springframework.stereotype.Service;
import java.security.SecureRandom;
import java.util.HashSet;
import java.util.Set;

/**
 * 自动生成VID的工具类
 */
public class Vid_Auto {

    private static final String CHARACTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
    private static final int STRING_LENGTH = 16;
    private static final SecureRandom RANDOM = new SecureRandom();

    // 模拟的集合，用于检查唯一性
    private static Set<String> existingStrings = new HashSet<>();

    public static String generateUniqueRandomString() {
        String randomString;
        do {
            randomString = generateRandomString();
        } while (existingStrings.contains(randomString));

        // 如果添加到集合中成功，则它是唯一的
        existingStrings.add(randomString);

        return randomString;
    }

    public static String generateRandomString() {
        StringBuilder builder = new StringBuilder(STRING_LENGTH);
        for (int i = 0; i < STRING_LENGTH; i++) {
            int index = RANDOM.nextInt(CHARACTERS.length());
            char randomChar = CHARACTERS.charAt(index);
            builder.append(randomChar);
        }
        return builder.toString();
    }

}